package com.chb.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chb.system.mapper.OperLogMapper;
import com.chb.system.service.OperLogService;
import com.chb.system.vo.OperLogVo;
import com.hos.entity.sys.OperLog;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author ：张家琪
 * @date ：Created in 2023/12/18 15:01
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class OperLogServiceImpl implements OperLogService {
    @Resource
    private OperLogMapper operLogMapper;

    @Override
    public List<OperLog> getAllOperLog() {
        LambdaQueryWrapper<OperLog> wrapper = new LambdaQueryWrapper<>();
        return operLogMapper.selectList(wrapper);
    }

    @Override
    public Page<OperLog> selectOperLogByCond(OperLogVo operLogVo) {
        Page<OperLog> page = new Page<>(operLogVo.getPageNum(), operLogVo.getPageSize());
        LambdaQueryWrapper<OperLog> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(operLogVo.getTiele())) {
            wrapper.like(OperLog::getTitle, operLogVo.getTiele());
        }
        if (StringUtils.hasText(operLogVo.getOperName())) {
            wrapper.like(OperLog::getOperName, operLogVo.getOperName());
        }
        if (StringUtils.hasText(operLogVo.getBusinessType())) {
            wrapper.eq(OperLog::getBusinessType, operLogVo.getBusinessType());
        }
        if (StringUtils.hasText(operLogVo.getStatus())) {
            wrapper.eq(OperLog::getStatus, operLogVo.getStatus());
        }
        if (Objects.nonNull(operLogVo.getDateRange())) {
            wrapper.between(OperLog::getOperTime, operLogVo.getDateRange()[0], operLogVo.getDateRange()[1]);
        }
        wrapper.orderByDesc(OperLog::getOperTime);
        return operLogMapper.selectPage(page, wrapper);
    }

    @Override
    public Integer deleteOperLogByIds(List<Long> ids) {
        return operLogMapper.deleteBatchIds(ids);
    }

    @Override
    public Integer clearOperLog() {
        return operLogMapper.delete(null);
    }

    /**
     * 增加操作日志
     *
     * @param operLog
     * @return
     */
    @Override
    public Integer addOperLog(OperLog operLog) {
        LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
        return operLogMapper.insert(operLog);
    }
}